home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / Draw / Sources / DrawPart.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  33.2 KB  |  1,198 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                DrawPart.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //
  8. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef DRAWPART_H
  15. #include "DrawPart.h"
  16. #endif
  17.  
  18. #ifndef DEFINES_K
  19. #include "Defines.k"
  20. #endif
  21.  
  22. #ifndef BINDING_K
  23. #include "Binding.k"
  24. #endif
  25.  
  26. #ifndef CONSTANT_H
  27. #include "Constant.h"
  28. #endif
  29.  
  30. #ifndef DEFINES_K
  31. #include "Defines.k"
  32. #endif
  33.  
  34. #ifndef DRAWCONT_H
  35. #include "DrawCont.h"
  36. #endif
  37.  
  38. #ifndef DRAWFRM_H
  39. #include "DrawFrm.h"
  40. #endif
  41.  
  42. #ifndef DRAWPRXY_H
  43. #include "DrawPrxy.h"
  44. #endif
  45.  
  46. #ifndef BASESHP_H
  47. #include "BaseShp.h"
  48. #endif
  49.  
  50. #ifndef BOUNDSHP_H
  51. #include "BoundShp.h"
  52. #endif
  53.  
  54. #ifndef LINESHP_H
  55. #include "LineShp.h"
  56. #endif
  57.  
  58. #ifndef OVALSHP_H
  59. #include "OvalShp.h"
  60. #endif
  61.  
  62. #ifndef RECTSHP_H
  63. #include "RectShp.h"
  64. #endif
  65.  
  66. #ifndef RRECTSHP_H
  67. #include "RRectShp.h"
  68. #endif
  69.  
  70. #ifndef TEXTSHP_H
  71. #include "TextShp.h"
  72. #endif
  73.  
  74. #ifndef DRAWSEL_H
  75. #include "DrawSel.h"
  76. #endif
  77.  
  78. #ifndef DRAWLINK_H
  79. #include "DrawLink.h"
  80. #endif
  81.  
  82. #ifndef TOOLFRM_H
  83. #include "ToolFrm.h"
  84. #endif
  85.  
  86. #ifndef PALFRAME_H
  87. #include "PalFrame.h"
  88. #endif
  89.  
  90. #ifndef PATFRAME_H
  91. #include "PatFrame.h"
  92. #endif
  93.  
  94. #ifndef DRAWCMDS_H
  95. #include "DrawCmds.h"
  96. #endif
  97.  
  98. #ifndef DRAWCLIP_H
  99. #include "DrawClip.h"
  100. #endif
  101.  
  102. #ifndef PRINTHDL_H
  103. #include "PrintHdl.h"
  104. #endif
  105.  
  106. #ifndef GROUPCMD_H
  107. #include "GroupCmd.h"
  108. #endif
  109.  
  110. #ifndef RULER_H
  111. #include "Ruler.h"
  112. #endif
  113.  
  114. #ifndef SOM_ODFExamples_ODFDrawEmbeddedFramesIterator_xh
  115. #include "SOMIter.xh"
  116. #endif
  117.  
  118. #ifndef FWABOUT_H
  119. #include "FWAbout.h"
  120. #endif
  121.  
  122. #ifndef FWSUUTIL_H
  123. #include "FWSUUtil.h"
  124. #endif
  125.  
  126. #ifndef FWPXYITE_H
  127. #include "FWPxyIte.h"
  128. #endif
  129.  
  130. #ifndef FWUTIL_H
  131. #include "FWUtil.h"
  132. #endif
  133.  
  134. #ifndef FWSCLBAR_H
  135. #include "FWSclBar.h"
  136. #endif
  137.  
  138. #ifndef FWPRESEN_H
  139. #include "FWPresen.h"
  140. #endif
  141.  
  142. // ----- OS Layer -----
  143.  
  144. #ifndef FWAPLEVT_H
  145. #include "FWAplEvt.h"
  146. #endif
  147.  
  148. #ifndef FWCOLORP_H
  149. #include "FWColorP.h"
  150. #endif
  151.  
  152. #ifndef FWRECT_H
  153. #include "FWRect.h"
  154. #endif
  155.  
  156. #ifndef FWRECSHP_H
  157. #include "FWRecShp.h"
  158. #endif
  159.  
  160. #ifndef FWWINDOW_H
  161. #include "FWWindow.h"
  162. #endif
  163.  
  164. #ifndef FWFLOWIN_H
  165. #include "FWFloWin.h"
  166. #endif
  167.  
  168. #ifndef FWEVENT_H
  169. #include "FWEvent.h"
  170. #endif
  171.  
  172. #ifndef FWMENU_H
  173. #include "FWMenu.h"
  174. #endif
  175.  
  176. #ifndef FWODGEOM_H
  177. #include "FWODGeom.h"
  178. #endif
  179.  
  180. #ifndef FWALERT_H
  181. #include "FWAlert.h"
  182. #endif
  183.  
  184. #ifndef FWCFMRES_H
  185. #include "FWCFMRes.h"
  186. #endif
  187.  
  188. #ifndef SLMixOS_H
  189. #include "SLMixOS.h"
  190. #endif
  191.  
  192. // ----- Foundation Includes -----
  193.  
  194. #ifndef FWSTREAM_H
  195. #include "FWStream.h"
  196. #endif
  197.  
  198. #ifndef FWSTRING_H
  199. #include "FWString.h"
  200. #endif
  201.  
  202. #ifndef FWSUSINK_H
  203. #include "FWSUSink.h"
  204. #endif
  205.  
  206. // ----- OpenDoc Includes -----
  207.  
  208. #ifndef SOM_ODArbitrator_xh
  209. #include <Arbitrat.xh>
  210. #endif
  211.  
  212. #ifndef SOM_ODDispatcher_xh
  213. #include <Disptch.xh>
  214. #endif
  215.  
  216. #ifndef SOM_ODShape_xh
  217. #include <Shape.xh>
  218. #endif
  219.  
  220. #ifndef SOM_ODStorageUnit_xh
  221. #include <StorageU.xh>
  222. #endif
  223.  
  224. #ifndef SOM_ODSession_xh
  225. #include <ODSessn.xh>
  226. #endif
  227.  
  228. #ifndef SOM_Module_OpenDoc_StdProps_defined
  229. #include <StdProps.xh>
  230. #endif
  231.  
  232. #ifndef SOM_ODInfo_xh
  233. #include <Info.xh>
  234. #endif
  235.  
  236. #ifndef FWARCSHP_H
  237. #include "FWArcShp.h"
  238. #endif
  239.  
  240. #ifndef FWPRINFO_H
  241. #include "FWPrInfo.h"
  242. #endif
  243.  
  244. #ifndef FWITERS_H
  245. #include "FWIters.h"
  246. #endif
  247.  
  248. //========================================================================================
  249. // Runtime Information
  250. //========================================================================================
  251.  
  252. #ifdef FW_BUILD_MAC
  253. #pragma segment odfdraw
  254. #endif
  255.  
  256. FW_DEFINE_CLASS_M2(CDrawPart, FW_CEmbeddingPart, FW_MEmbeddingPartScriptable)
  257. FW_DEFINE_AUTO(CDrawPart)
  258.  
  259. //========================================================================================
  260. //    class CDrawPart
  261. //========================================================================================
  262.  
  263. //----------------------------------------------------------------------------------------
  264. //    CDrawPart::CDrawPart
  265. //----------------------------------------------------------------------------------------
  266.  
  267. CDrawPart::CDrawPart(ODPart* odPart) :
  268.     FW_CEmbeddingPart(odPart, FW_gInstance, kPartInfoID),
  269.     FW_MEmbeddingPartScriptable(),
  270.     FW_MReceiver(),
  271.     fNbEmbedded(0),    
  272.     fPartContent(NULL),
  273.     fCurrentPenSize(FW_kFixedPos1),
  274.     fRenderVerb(kFillFrame),
  275.     fPaletteWindow(NULL),
  276.     fPatternWindow(NULL),
  277.     fToolsWindow(NULL),
  278.     fToolFrame(NULL),
  279.     fPaletteFrame(NULL),
  280.     fPatternFrame(NULL),
  281.     fPalettePresentation(NULL),
  282.     fPatternPresentation(NULL),
  283.     fToolPresentation(NULL),
  284.     fMainPresentation(NULL),
  285.     fAlignObjectsPresentation(NULL),    
  286.     fTool(kSelectTool),    
  287.     fFillColor(FW_kRGBWhite),
  288.     fFrameColor(FW_kRGBBlack),
  289.     fAutoGrid(TRUE),
  290.     fUnit(cInchUnit),
  291.     fArcUnit(cDegreesUnit),
  292.     fPostCreated(FALSE)
  293. {
  294.     
  295.     FW_END_CONSTRUCTOR
  296. }
  297.  
  298. //----------------------------------------------------------------------------------------
  299. //    CDrawPart::~CDrawPart
  300. //----------------------------------------------------------------------------------------
  301.  
  302. CDrawPart::~CDrawPart()
  303. {
  304.     FW_START_DESTRUCTOR
  305. }
  306.  
  307. //----------------------------------------------------------------------------------------
  308. //    CDrawPart::ReleaseAll
  309. //----------------------------------------------------------------------------------------
  310.  
  311. void CDrawPart::ReleaseAll(Environment *ev)
  312. {
  313.     FW_CEmbeddingPart::ReleaseAll(ev);
  314.     
  315.     delete fPaletteWindow;
  316.     delete fPatternWindow;
  317.     delete fToolsWindow;
  318. }
  319.  
  320. //----------------------------------------------------------------------------------------
  321. //    CDrawPart::Initialize
  322. //----------------------------------------------------------------------------------------
  323.  
  324. void CDrawPart::Initialize(Environment *ev, ODStorageUnit* storageUnit, FW_Boolean fromStorage)
  325. {
  326.     // ----- Call Inherited Initialize -----
  327.     FW_CEmbeddingPart::Initialize(ev, storageUnit, fromStorage);
  328.  
  329.     fDrawingSize.Set(FW_IntToFixed(kDrawingSizeX), FW_IntToFixed(kDrawingSizeY));
  330.  
  331. #ifdef FW_BUILD_WIN
  332.     // [HLX] Temporary embed menu for windows
  333.     WinAddEmbedMenu(ev, GetMenuBar(ev));
  334. #endif
  335.  
  336.     // ----- We are using FW_CToggleItem and FW_CSubMenuItem so avoid dead stripping -----
  337.     FW_DO_NOT_DEAD_STRIP(FW_CToggleItem);
  338.     FW_DO_NOT_DEAD_STRIP(FW_CSubMenuItem);
  339.  
  340.     // ----- Tokenize -----
  341.     fDrawSelection = FW_NEW(CDrawSelection, (ev, this));    // Attention I don't own the selection the presentation will
  342.     
  343.     fToolPresentation             = RegisterPresentation(ev, kToolPresentation, FALSE);
  344.     fPalettePresentation         = RegisterPresentation(ev, kPalettePresentation, FALSE);
  345.     fPatternPresentation         = RegisterPresentation(ev, kPatternPresentation, FALSE);
  346.     fMainPresentation             = RegisterPresentation(ev, kMainPresentation, TRUE, kDrawViewNotRoot, kDrawView, fDrawSelection);
  347.     fAlignObjectsPresentation     = RegisterPresentation(ev, kAlignObjectsPresentation, FALSE);
  348.     
  349.     InitializePalettes(ev);
  350.  
  351.     // ----- Pattern Table -----
  352.     fPatternTable[0]     = FW_kBlackPat;
  353.     fPatternTable[1]     = FW_kWhitePat;
  354.     fPatternTable[2]     = FW_kGrayPat;
  355.     fPatternTable[3]     = FW_kLightGrayPat;
  356.     fPatternTable[4]     = FW_kDarkGrayPat;
  357.     fPatternTable[5]     = FW_kHorizontalPat;
  358.     fPatternTable[6]     = FW_kVerticalPat;
  359.     fPatternTable[7]     = FW_kFDiagonalPat;
  360.     fPatternTable[8]     = FW_kBDiagonalPat;
  361.     fPatternTable[9]     = FW_kCrossPat;
  362.     fPatternTable[10]     = FW_kDiagCrossPat;
  363.     fPatternTable[11]     = FW_kAntPat;
  364.     
  365.     fFillPatIndex = 0;
  366.     fFramePatIndex = 0;
  367.     
  368.     // ----- Register PICT type -----
  369.     RegisterKind(ev, 'PICT', kODPlatformDataType, FW_kDataInterchangeStorage, FW_kExportEnabled);
  370. }
  371.  
  372. //------------------------------------------------------------------------------
  373. //    CDrawPart::SetDrawingSize
  374. //------------------------------------------------------------------------------
  375.  
  376. void CDrawPart::SetDrawingSize(Environment* ev, const FW_CPoint& newSize)
  377. {
  378.     if(fDrawingSize != newSize)
  379.     {
  380.         fDrawingSize = newSize;
  381.  
  382.         FW_CPresentationFrameIterator ite(ev, fMainPresentation);
  383.         for (CDrawFrame* theFrame = (CDrawFrame*) ite.First(ev); ite.IsNotComplete(ev); theFrame = (CDrawFrame*) ite.Next(ev))
  384.         {
  385.             theFrame->AdjustContentViewSize(ev, FW_kInvalidate);
  386.  
  387.             CRuler* horzRuler = (CRuler*) theFrame->FindViewByID(ev, kHorzRulerID);
  388.             CRuler* vertRuler = (CRuler*) theFrame->FindViewByID(ev, kVertRulerID);
  389.             
  390.             FW_CPoint extent;
  391.  
  392.             extent = horzRuler->GetExtent(ev);
  393.             extent.x = newSize.x;
  394.             horzRuler->SetExtent(ev, extent);
  395.  
  396.             extent = vertRuler->GetExtent(ev);
  397.             extent.y = newSize.y;
  398.             vertRuler->SetExtent(ev, extent);
  399.  
  400.             theFrame->GetContentView(ev)->SetExtent(ev, fDrawingSize);
  401.             
  402.             theFrame->Invalidate(ev);
  403.         }    
  404.     }
  405. }
  406.  
  407. //------------------------------------------------------------------------------
  408. //    CDrawPart::InitializePalettes
  409. //------------------------------------------------------------------------------
  410.  
  411. void CDrawPart::InitializePalettes(Environment *ev)
  412. {
  413.     FW_CPoint position(FW_IntToFixed(100), FW_IntToFixed(100));
  414.     fPatternWindow = new FW_CFloatingWindow(ev, 
  415.                                             this, 
  416.                                             fPatternPresentation, 
  417.                                             FW_CString32(""), 
  418.                                             FW_kZeroPoint, 
  419.                                             position,
  420.                                             TRUE);
  421.  
  422.     fPaletteWindow = new FW_CFloatingWindow(ev, 
  423.                                             this,
  424.                                             fPalettePresentation, 
  425.                                             FW_CString32(""), 
  426.                                             FW_kZeroPoint, 
  427.                                             position,
  428.                                             TRUE);
  429.  
  430.     FW_CRect screenBounds;
  431.     ::FW_GetMainScreenBounds(screenBounds);
  432.     screenBounds.Inset(FW_IntToFixed(3), FW_IntToFixed(3));
  433.  
  434.     FW_CPoint windowSize(FW_IntToFixed(kToolsWindowWidth), FW_IntToFixed(kToolsWindowHeight));
  435.     fToolsWindow = new FW_CFloatingWindow(ev, 
  436.                                             this, 
  437.                                             fToolPresentation, 
  438.                                             FW_CString32(""), 
  439.                                             windowSize, 
  440.                                             screenBounds.TopLeft(),
  441.                                             TRUE);
  442. }
  443.  
  444. //----------------------------------------------------------------------------------------
  445. //    CDrawPart::OnAcquireSelectionFocus
  446. //----------------------------------------------------------------------------------------
  447.  
  448. void CDrawPart::OnAcquireSelectionFocus(Environment* ev, CDrawFrame* frame)
  449. {
  450.     static short done = false;
  451.     
  452.     if (!done && frame->IsRoot(ev) && !fToolsWindow->IsShown(ev))
  453.     {
  454.         done = TRUE;
  455.         fToolsWindow->ShowHide(ev, TRUE);
  456.     }
  457. }
  458.  
  459. //----------------------------------------------------------------------------------------
  460. //    CDrawPart::NewDocumentWindow
  461. //----------------------------------------------------------------------------------------
  462.  
  463. FW_CWindow* CDrawPart::NewDocumentWindow(Environment* ev)
  464. {
  465.     FW_CPoint sbSize = FW_CScrollBar::GetDefaultScrollBarSize();
  466.  
  467.     // ----- Add the rulers -----
  468.     sbSize.x += kRulerWidth;
  469.     sbSize.y += kRulerWidth;
  470.     
  471.     FW_CPoint drawingSize(fDrawingSize.x + sbSize.x, fDrawingSize.y + sbSize.y);
  472.         
  473.     FW_CRect screenBounds;
  474.     ::FW_GetMainScreenBounds(screenBounds);
  475.     screenBounds.Inset(FW_IntToFixed(3), FW_IntToFixed(3));
  476.     
  477.     // Make room for the tool palettes
  478.     screenBounds.left += FW_IntToFixed(3 + kToolsWindowWidth + 3);
  479.     
  480.     return new FW_CWindow(ev,
  481.                         this,
  482.                          FW_CPart::fgViewAsFrameToken,
  483.                         fMainPresentation,
  484.                         drawingSize,
  485.                         screenBounds.TopLeft(),
  486.                         FW_kDocumentWindow);
  487. }
  488.  
  489. //---------------------------------------------------------------------------------------
  490. //    FW_CPart::InternalizeContent
  491. //---------------------------------------------------------------------------------------
  492. //    I override FW_CPart::InternalizeContent because I want to read my annotations
  493.  
  494. FW_Boolean CDrawPart::InternalizeContent(Environment* ev, ODStorageUnit* storageUnit, FW_CCloneInfo* cloneInfo)
  495. {
  496.     FW_Boolean result = FW_CEmbeddingPart::InternalizeContent(ev, storageUnit, cloneInfo);
  497.     
  498.     // ----- Read general information -----
  499.     if (result && storageUnit->Exists(ev, kContentAnnotationProp, (ODValueType)kContentAnnotationValue, 0))
  500.     {
  501.         FW_PStorageUnitSink suSink(ev, storageUnit, kContentAnnotationProp, kContentAnnotationValue);
  502.         FW_PBufferedSink sink(ev, suSink);
  503.         FW_CReadableStream stream(sink);
  504.         stream >> fCurrentPenSize;
  505.         stream >> fRenderVerb;
  506.         stream >> fAutoGrid;
  507.     }
  508.         
  509.     return result;
  510. }
  511.  
  512. //---------------------------------------------------------------------------------------
  513. //    FW_CPart::ExternalizeContent
  514. //---------------------------------------------------------------------------------------
  515. //    I override FW_CPart::ExternalizeContent because I want to write my annotations
  516.  
  517. void CDrawPart::ExternalizeContent(Environment* ev, ODStorageUnit* storageUnit, FW_CCloneInfo* cloneInfo)
  518. {
  519.     FW_CEmbeddingPart::ExternalizeContent(ev, storageUnit, cloneInfo);
  520.     
  521.     // ----- Write general information -----
  522.     //    FW_SUAddPropValue correctly test if the property and value exist before
  523.     //    trying to create them
  524.     FW_SUAddPropValue(ev, storageUnit, kContentAnnotationProp, kContentAnnotationValue);
  525.  
  526.     FW_PStorageUnitSink suSink(ev, storageUnit, kContentAnnotationProp, kContentAnnotationValue);
  527.     FW_CWritableStream stream(suSink);
  528.     stream << fCurrentPenSize;
  529.     stream << fRenderVerb;
  530.     stream << fAutoGrid;
  531. }
  532.  
  533. //---------------------------------------------------------------------------------------
  534. //    CDrawPart::PrintInfoChanged
  535. //---------------------------------------------------------------------------------------
  536.  
  537. void CDrawPart::PrintInfoChanged(Environment *ev, FW_CPrintInfo* printInfo)
  538. {
  539.     if(printInfo != nil)
  540.     {
  541.         FW_CRect pageRect = printInfo->GetPageBounds();
  542.         FW_CRect paperRect = printInfo->GetPaperBounds();
  543.         
  544.         FW_CPoint newSize(pageRect.Width(), pageRect.Height());
  545.         
  546.         long hRes, vRes;
  547.         
  548.         // print bounds are based on current printer resolution,
  549.         // but our coordinate system is pixels
  550.         if(printInfo->GetPrinterResolution(hRes, vRes))
  551.         {
  552.             newSize.x = newSize.x / (FW_IntToFixed(hRes) / FW_IntToFixed(72));
  553.             newSize.y = newSize.y / (FW_IntToFixed(vRes) / FW_IntToFixed(72));
  554.         }
  555.         
  556.         SetDrawingSize(ev, newSize);
  557.     }
  558. }
  559.  
  560. //----------------------------------------------------------------------------------------
  561. //    CDrawPart::NewPartContent
  562. //----------------------------------------------------------------------------------------
  563.  
  564. FW_CContent* CDrawPart::NewPartContent(Environment* ev)
  565. {
  566.     fPartContent = FW_NEW(CDrawPartContent, (ev, this));
  567.     return fPartContent;
  568. }
  569.  
  570. //----------------------------------------------------------------------------------------
  571. //    CDrawPart::NewLinkManager
  572. //----------------------------------------------------------------------------------------
  573. FW_CLinkManager* CDrawPart::NewLinkManager(Environment *ev)
  574. {
  575.     CDrawLinkManager* linkMgr = FW_NEW(CDrawLinkManager, (ev, this));
  576.     return linkMgr;
  577. }
  578.  
  579. //----------------------------------------------------------------------------------------
  580. //    CDrawPart::AddShapeToPart
  581. //----------------------------------------------------------------------------------------
  582.  
  583. void CDrawPart::AddShapeToPart(Environment* ev, CBaseShape* shape)
  584. {
  585.     fPartContent->AddShape(ev, shape);
  586. }
  587.  
  588. //----------------------------------------------------------------------------------------
  589. //    CDrawPart::RemoveShapeFromPart
  590. //----------------------------------------------------------------------------------------
  591.  
  592. void CDrawPart::RemoveShapeFromPart(Environment* ev, CBaseShape* shape)
  593. {
  594.     fPartContent->RemoveShape(ev, shape);
  595.     shape->Removed(ev);
  596. }
  597.  
  598. //----------------------------------------------------------------------------------------
  599. //    CDrawPart::DoAbout
  600. //----------------------------------------------------------------------------------------
  601.  
  602. FW_Handled CDrawPart::DoAbout(Environment* ev)
  603. {
  604.     ::FW_About(ev, this, kAbout);
  605.     
  606.     return FW_kHandled;
  607. }
  608.  
  609. //----------------------------------------------------------------------------------------
  610. //    CDrawPart::DoMenu
  611. //----------------------------------------------------------------------------------------
  612.  
  613. FW_Handled CDrawPart::DoMenu(Environment* ev, const FW_CMenuEvent& theMenuEvent)
  614. {
  615.     FW_CString32 name("ODFDraw");
  616.     FW_CString32 sorry("Sorry, not yet implemented!");
  617.  
  618.     FW_Handled result = FW_kHandled;
  619.     ODCommandID commandID = theMenuEvent.GetCommandID(ev);
  620.     
  621.     if (cPen1 <= commandID && commandID <= cPen5)
  622.     {
  623.         int temp = cPen1;
  624.         FW_Fixed penSize = FW_IntToFixed(commandID - temp + 1);
  625.         if (!fDrawSelection->IsEmpty(ev))
  626.         {
  627.             CChangePenSizeCommand* cmd = FW_NEW(CChangePenSizeCommand, (ev, this, this->GetLastActiveFrame(ev), fDrawSelection, penSize));
  628.             cmd->Execute(ev);
  629.         }
  630.         else
  631.             fCurrentPenSize = penSize;
  632.     }
  633.     else
  634.     {
  635.         switch (commandID)
  636.         {
  637.             case cHideShowPattern:
  638.                 fPatternWindow->ShowHide(ev, !fPatternWindow->IsShown(ev));
  639.                 break;
  640.                 
  641.             case cHideShowPalette:
  642.                 fPaletteWindow->ShowHide(ev, !fPaletteWindow->IsShown(ev));
  643.                 break;
  644.             
  645.             case cHideShowTools:
  646.                 fToolsWindow->ShowHide(ev, !fToolsWindow->IsShown(ev));
  647.                 break;
  648.  
  649.             case cMoveForward:
  650.                 if (fPartContent->MoveForward(ev))
  651.                 {
  652.                     UpdateAfterMove(ev);
  653.                     this->Changed(ev);
  654.                 }
  655.                 break;
  656.             case cMoveToFront:
  657.                 fPartContent->MoveToFront(ev);
  658.                 UpdateAfterMove(ev);
  659.                 this->Changed(ev);
  660.                 break;
  661.             case cMoveBackward:
  662.                 if (fPartContent->MoveBackward(ev))
  663.                 {
  664.                     UpdateAfterMove(ev);
  665.                     this->Changed(ev);
  666.                 }
  667.                 break;
  668.             case cMoveToBack:
  669.                 fPartContent->MoveToBack(ev);
  670.                 UpdateAfterMove(ev);
  671.                 this->Changed(ev);
  672.                 break;
  673.             
  674.             case cAutoGrid:
  675.                 fAutoGrid = !fAutoGrid;
  676.                 break;
  677.  
  678.             case cGroup:
  679.               {
  680.                 CGroupShapesCommand* cmd = FW_NEW(CGroupShapesCommand, (ev, this->GetLastActiveFrame(ev), fDrawSelection));
  681.                 cmd->Execute(ev);
  682.               }
  683.                 break;
  684.             case cUngroup:
  685.               {
  686.                 CUngroupShapesCommand* cmd = FW_NEW(CUngroupShapesCommand, (ev, this->GetLastActiveFrame(ev), fDrawSelection));
  687.                 cmd->Execute(ev);
  688.               }
  689.                 break;
  690.                 
  691.             case cAlignObjects:
  692.             case cAlignToGrid:
  693.             case cRotate:
  694.             case cFlipH:
  695.             case cFlipV:
  696.             case cLock:
  697.             case cUnlock:
  698.             case cScaleSelection:
  699.             case cRoundCorners:
  700.                 FW_NoteAlert(name, sorry);
  701.                 break;
  702.  
  703.             case cInchUnit:
  704.             case cCmUnit:
  705.             case cPixelUnit:
  706.             case cPicaUnit:
  707.                 fUnit = commandID;
  708.                 FW_NoteAlert(name, sorry);
  709.                 break;
  710.                 
  711.             case cDegreesUnit:
  712.             case cRadiansUnit:
  713.                 fArcUnit = commandID;
  714.                 FW_NoteAlert(name, sorry);
  715.                 break;
  716.  
  717.             default:
  718.                 result = FW_kNotHandled;
  719.         }    
  720.     }
  721.     
  722.     return result;
  723. }
  724.  
  725. //----------------------------------------------------------------------------------------
  726. //    CDrawPart::SetTool
  727. //----------------------------------------------------------------------------------------
  728. void CDrawPart::SetTool(Environment* ev, unsigned short newTool)
  729. {
  730.     if (fTool != newTool)
  731.     {
  732.         if (fTool == kSelectTool && !fDrawSelection->IsEmpty(ev))
  733.             fDrawSelection->CloseSelection(ev);
  734.         
  735.         CheckTool(ev, fTool, newTool);
  736.         fTool = newTool;        
  737.     }
  738. }
  739.  
  740. //----------------------------------------------------------------------------------------
  741. //    CDrawPart::SetRenderVerb
  742. //----------------------------------------------------------------------------------------
  743.  
  744. void CDrawPart::SetRenderVerb(Environment* ev, unsigned short renderVerb)
  745. {
  746.     if (fRenderVerb != renderVerb)
  747.     {        
  748.         if (!fDrawSelection->IsEmpty(ev))
  749.         {
  750.             CChangeRenderVerbCommand* cmd = FW_NEW(CChangeRenderVerbCommand,
  751.                                                     (ev, this, this->GetLastActiveFrame(ev), fDrawSelection, renderVerb));
  752.             cmd->Execute(ev);
  753.         }
  754.  
  755.         unsigned short oldRenderVerb = fRenderVerb;
  756.         fRenderVerb = renderVerb;        
  757.         CheckRenderVerb(ev, oldRenderVerb, fRenderVerb);
  758.     }
  759. }
  760.  
  761. //----------------------------------------------------------------------------------------
  762. //    CDrawPart::NewFrame
  763. //----------------------------------------------------------------------------------------
  764.  
  765. FW_CFrame* CDrawPart::NewFrame(Environment* ev, 
  766.                                 ODFrame* odFrame, 
  767.                                 FW_CPresentation* presentation,
  768.                                 FW_Boolean fromStorage)
  769. {
  770. FW_UNUSED(fromStorage);
  771.     if (presentation == fPalettePresentation)
  772.     {
  773.         if (fPaletteFrame)
  774.             return fPaletteFrame;
  775.         else
  776.         {
  777.             fPaletteFrame = FW_NEW(CPaletteFrame, (ev, odFrame, presentation, this));
  778.             
  779.             CColorChangedInterest interest(fPaletteFrame);
  780.             AddInterest(interest);            
  781.                 
  782.             return fPaletteFrame;
  783.         }
  784.     }
  785.     else if (presentation == fPatternPresentation)
  786.     {
  787.         if (fPatternFrame)
  788.             return fPatternFrame;
  789.         else
  790.         {
  791.             fPatternFrame = FW_NEW(CPatternFrame, (ev, odFrame, presentation, this));
  792.             
  793.             CPatternChangedInterest interest(fPatternFrame);
  794.             AddInterest(interest);
  795.                         
  796.             return fPatternFrame;
  797.         }
  798.     }
  799.     else if (presentation == fToolPresentation)
  800.     {
  801.         if (fToolFrame)
  802.             return fToolFrame;
  803.         else
  804.         {
  805.             fToolFrame = FW_NEW(CToolFrame, (ev, odFrame, presentation, this));
  806.             
  807.             CColorChangedInterest colorInterest(this);
  808.             fToolFrame->AddInterest(colorInterest);
  809.             CPatternChangedInterest patternInterest(this);
  810.             fToolFrame->AddInterest(patternInterest);
  811.  
  812.             return fToolFrame;
  813.         }
  814.     }
  815.     
  816.     return FW_NEW(CDrawFrame, (ev, odFrame, presentation, this));
  817. }
  818.  
  819. //----------------------------------------------------------------------------------------
  820. //    CDrawPart::DoAdjustMenus
  821. //----------------------------------------------------------------------------------------
  822.  
  823. FW_Handled CDrawPart::DoAdjustMenus(Environment* ev, FW_CMenuBar* menuBar, FW_Boolean hasMenuFocus, FW_Boolean isRoot)
  824. {
  825. FW_UNUSED(isRoot);
  826.     if (!hasMenuFocus)
  827.         return FW_kNotHandled;
  828.         
  829.     ODCommandID command;
  830.     FW_Boolean hasSelection = fDrawSelection->Count() > 0;
  831.  
  832.     // ----- Edit Menu -----
  833.     menuBar->EnableCommand(ev, kODCommandSelectAll, fPartContent->CountShapes() > 0);
  834.  
  835.     // ----- PenSize Menu -----
  836.     FW_Fixed penSize = fCurrentPenSize;
  837.     if (fDrawSelection->Count() >= 1)
  838.     {
  839.         if (!fDrawSelection->GetSelectionPenSize(ev, penSize))
  840.         {
  841.             penSize = FW_kFixed0;
  842.         }
  843.     }
  844.     command = FW_FixedToInt(penSize) + cPen1 - 1;
  845.     
  846.     for (ODCommandID p = cPen1; p <= cPen5; p++)
  847.         menuBar->EnableAndCheckCommand(ev, p, TRUE, p == command);
  848.     
  849.     FW_Boolean readWrite = !IsReadOnly(ev);
  850.     
  851.     // ----- Arrange Menu -----
  852.     menuBar->EnableCommand(ev, cMoveForward, readWrite && hasSelection);
  853.     menuBar->EnableCommand(ev, cMoveToFront, readWrite && hasSelection);
  854.     menuBar->EnableCommand(ev, cMoveBackward, readWrite && hasSelection);
  855.     menuBar->EnableCommand(ev, cMoveToBack, readWrite && hasSelection);
  856.     menuBar->EnableCommand(ev, cAlignToGrid, readWrite && hasSelection);
  857.     menuBar->EnableCommand(ev, cAlignObjects, readWrite && hasSelection);
  858.     menuBar->EnableCommand(ev, cRotate, readWrite && hasSelection);
  859.     menuBar->EnableCommand(ev, cFlipH, readWrite && hasSelection);
  860.     menuBar->EnableCommand(ev, cFlipV, readWrite && hasSelection);
  861.     menuBar->EnableCommand(ev, cGroup, readWrite && hasSelection);
  862.     menuBar->EnableCommand(ev, cUngroup, readWrite && hasSelection);
  863.     menuBar->EnableCommand(ev, cLock, readWrite && hasSelection);
  864.     menuBar->EnableCommand(ev, cUnlock, readWrite && hasSelection);
  865.         
  866.     // ----- Options Menu -----
  867.     menuBar->EnableAndToggleCommand(ev, cAutoGrid, TRUE, !fAutoGrid);
  868.     menuBar->EnableCommand(ev, cScaleSelection, readWrite && hasSelection);
  869.     menuBar->EnableCommand(ev, cRoundCorners, TRUE);
  870.  
  871.     // ----- Units Menu -----
  872.     menuBar->EnableAndCheckCommand(ev, cInchUnit, TRUE, fUnit == cInchUnit);
  873.     menuBar->EnableAndCheckCommand(ev, cCmUnit, TRUE, fUnit == cCmUnit);
  874.     menuBar->EnableAndCheckCommand(ev, cPixelUnit, TRUE, fUnit == cPixelUnit);
  875.     menuBar->EnableAndCheckCommand(ev, cPicaUnit, TRUE, fUnit == cPicaUnit);
  876.     menuBar->EnableAndCheckCommand(ev, cDegreesUnit, TRUE, fArcUnit == cDegreesUnit);
  877.     menuBar->EnableAndCheckCommand(ev, cRadiansUnit, TRUE, fArcUnit == cRadiansUnit);
  878.  
  879.     // ----- Window Menu -----
  880.     menuBar->EnableAndToggleCommand(ev, cHideShowTools, TRUE, fToolsWindow == NULL || !fToolsWindow->IsShown(ev));
  881.     menuBar->EnableAndToggleCommand(ev, cHideShowPalette, TRUE, fPaletteWindow == NULL || !fPaletteWindow->IsShown(ev));
  882.     menuBar->EnableAndToggleCommand(ev, cHideShowPattern, TRUE, fPatternWindow == NULL || !fPatternWindow->IsShown(ev));    
  883.  
  884.     return FW_kNotHandled;
  885. }
  886.  
  887. //----------------------------------------------------------------------------------------
  888. //    CDrawPart::CheckTool
  889. //----------------------------------------------------------------------------------------
  890.  
  891. void CDrawPart::CheckTool(Environment* ev, unsigned short oldTool, unsigned short newTool)
  892. {
  893.     if (fToolFrame != NULL)
  894.         fToolFrame->CheckTool(ev, oldTool, newTool);
  895. }
  896.  
  897. //----------------------------------------------------------------------------------------
  898. //    CDrawPart::CheckRenderVerb
  899. //----------------------------------------------------------------------------------------
  900.  
  901. void CDrawPart::CheckRenderVerb(Environment* ev, unsigned short oldRenderVerb, unsigned short newRenderVerb)
  902. {
  903.     if (fToolFrame != NULL)
  904.         fToolFrame->CheckRenderVerb(ev, oldRenderVerb, newRenderVerb);
  905. }
  906.  
  907. //----------------------------------------------------------------------------------------
  908. //    CDrawPart::UpdateAfterMove
  909. //----------------------------------------------------------------------------------------
  910.  
  911. void CDrawPart::UpdateAfterMove(Environment* ev)
  912. {
  913.     CDrawFacetClipper facetClipper(this);
  914.     facetClipper.Clip(ev, fMainPresentation, fDrawSelection->GetUpdateShape());
  915.     
  916.     fMainPresentation->Invalidate(ev, fDrawSelection->GetUpdateShape());    
  917. }
  918.  
  919. //----------------------------------------------------------------------------------------
  920. //    CDrawPart::NotifyForColorChanged
  921. //----------------------------------------------------------------------------------------
  922.  
  923. void CDrawPart::NotifyForColorChanged(Environment *ev, const FW_CColor& color, FW_ERenderVerbs renderVerb)
  924. {
  925.     CColorChangedInterest interest(this);
  926.     CColorChangedNotification notification(interest, color, renderVerb);
  927.     Notify(ev, notification);
  928. }
  929.  
  930. //----------------------------------------------------------------------------------------
  931. //    CDrawPart::NotifyForPatternChanged
  932. //----------------------------------------------------------------------------------------
  933.  
  934. void CDrawPart::NotifyForPatternChanged(Environment *ev, short patIndex, FW_ERenderVerbs renderVerb)
  935. {
  936.     CPatternChangedInterest interest(this);
  937.     CPatternChangedNotification notification(interest, patIndex, renderVerb);
  938.     Notify(ev, notification);
  939. }
  940.  
  941. //----------------------------------------------------------------------------------------
  942. //    CDrawPart::SetColor
  943. //----------------------------------------------------------------------------------------
  944.  
  945. void CDrawPart::SetColor(Environment *ev, const FW_CColor& color, FW_ERenderVerbs renderVerb)
  946. {
  947.     if (fDrawSelection->IsEmpty(ev))
  948.     {
  949.         if (renderVerb == FW_kFrame)
  950.             fFrameColor = color;
  951.         else
  952.             fFillColor = color;
  953.         NotifyForColorChanged(ev, color, renderVerb);
  954.     }
  955.     else
  956.     {
  957.         CChangeColorCommand* cmd = FW_NEW(CChangeColorCommand,
  958.                                             (ev, (renderVerb == FW_kFrame ? cChangeFrameColor: cChangeFillColor), this, this->GetLastActiveFrame(ev), fDrawSelection, color));
  959.         cmd->Execute(ev);
  960.     }
  961. }
  962.  
  963. //----------------------------------------------------------------------------------------
  964. //    CDrawPart::SetPattern
  965. //----------------------------------------------------------------------------------------
  966.  
  967. void CDrawPart::SetPattern(Environment *ev, short newFillPatIndex, FW_ERenderVerbs renderVerb)
  968. {
  969.     if (fDrawSelection->IsEmpty(ev))
  970.     {
  971.         if (renderVerb == FW_kFrame)
  972.             fFramePatIndex = newFillPatIndex;
  973.         else
  974.             fFillPatIndex = newFillPatIndex;
  975.         NotifyForPatternChanged(ev, newFillPatIndex, renderVerb);
  976.     }
  977.     else
  978.     {
  979.         FW_CPattern pattern = GetPattern(newFillPatIndex).Copy();
  980.         CChangePatternCommand* cmd = FW_NEW(CChangePatternCommand,
  981.                                                 (ev, (renderVerb == FW_kFrame ? cChangeFramePattern : cChangeFillPattern), this, this->GetLastActiveFrame(ev), fDrawSelection, pattern));
  982.         cmd->Execute(ev);
  983.     }
  984. }
  985.  
  986. //----------------------------------------------------------------------------------------
  987. // CDrawPart::WhichShape
  988. //----------------------------------------------------------------------------------------
  989.  
  990. CBaseShape* CDrawPart::WhichShape(Environment *ev, FW_CGraphicContext& gc, const FW_CMouseEvent& theMouseEvent, FW_Boolean hasToBeSelected) const
  991. {
  992.     CDrawContentShapeIterator ite(fPartContent);
  993.     for (CBaseShape* shape = ite.Last(); ite.IsNotComplete(); shape = ite.Previous())
  994.     {
  995.         FW_Boolean test = (!hasToBeSelected) || (hasToBeSelected && shape->IsSelectedShape());
  996.         if (test && shape->HitTest(ev, gc, theMouseEvent))
  997.             return shape;    
  998.     }
  999.     
  1000.     return NULL;
  1001. }
  1002.  
  1003. //----------------------------------------------------------------------------------------
  1004. // CDrawPart::FindShapeWithIndex
  1005. //----------------------------------------------------------------------------------------
  1006. CBaseShape* CDrawPart::FindShapeWithIndex(short extIndex)
  1007. {
  1008.     // Search the shape list and return the shape with the specified externalization index
  1009.     CDrawContentShapeIterator ite(fPartContent);
  1010.     for (CBaseShape* shape = ite.First(); ite.IsNotComplete(); shape = ite.Next())
  1011.     {
  1012.         if (shape->GetExternalizationIndex() == extIndex)
  1013.             return shape;
  1014.     }    
  1015.  
  1016.     // extIndex not found!?
  1017.     return NULL;
  1018. }
  1019.  
  1020. //----------------------------------------------------------------------------------------
  1021. // CDrawPart::DoPostCreate
  1022. //----------------------------------------------------------------------------------------
  1023. void CDrawPart::DoPostCreate(Environment* ev)
  1024. {
  1025.     // Perform "DoPostCreate"-type initialization.
  1026.     // In our case, we want to make sure that any subscribers that were just internalized 
  1027.     // get registered for automatic updates.
  1028.  
  1029.     if (!fPostCreated)    // do this only once!
  1030.     {
  1031.         CDrawLinkManager* linkMgr = (CDrawLinkManager*) this->GetLinkManager(ev);
  1032.         linkMgr->RegisterLinks(ev);
  1033.         fPostCreated = TRUE;
  1034.     }
  1035. }
  1036.  
  1037. //----------------------------------------------------------------------------------------
  1038. // CDrawPart::NewElementIterator
  1039. //----------------------------------------------------------------------------------------
  1040.  
  1041. FW_CElementIterator* CDrawPart::NewElementIterator(Environment* ev,
  1042.                                                 FW_CPart* part,
  1043.                                                 ODDescType desiredClass) const
  1044. {
  1045.     FW_CElementIterator* iter;
  1046.     
  1047.     unsigned short shapeType;
  1048.     
  1049.     if (desiredClass == typeWildCard || desiredClass == kShapeClass || ShapeClassToType(desiredClass, shapeType))
  1050.         iter = FW_NEW(CSemanticShapeElementIterator, (fPartContent, desiredClass));
  1051.     else
  1052.         iter = FW_MEmbeddingPartScriptable::NewElementIterator(ev, part, desiredClass);
  1053.         
  1054.     return iter;
  1055. }
  1056.  
  1057. //----------------------------------------------------------------------------------------
  1058. // CDrawPart::DoAECreateElement
  1059. //----------------------------------------------------------------------------------------
  1060.  
  1061. void CDrawPart::DoAECreateElement(Environment* ev,
  1062.                                 FW_CPart* part,
  1063.                                 const FW_CAppleEvent& event,
  1064.                                 FW_CAppleEvent& reply)
  1065. {
  1066.     ODDescType         shapeClass = event.GetType(keyAEObjectClass);
  1067.     unsigned short     shapeType;
  1068.     
  1069.     if (ShapeClassToType(shapeClass, shapeType))
  1070.     {
  1071.         CBaseShape* theShape = GetDrawContent()->NewShape(ev, shapeType);
  1072.         FW_CColor color;
  1073.     
  1074.         theShape->SetFrameInk(FW_kNormalInk);
  1075.         theShape->SetFrameStyle(FW_kNormalStyle);
  1076.     
  1077.         theShape->ChangeRenderVerb(ev, GetRenderVerb());
  1078.         theShape->ChangePenSize(ev, GetPenSize());
  1079.             
  1080.         GetFrameColor(color);
  1081.         theShape->ChangeFrameColor(ev, color);
  1082.  
  1083.         GetFillColor(color);
  1084.         theShape->ChangeFillColor(ev, color);
  1085.     
  1086.         theShape->ChangeFramePattern(ev, GetFramePattern());
  1087.         theShape->ChangeFillPattern(ev, GetFillPattern());
  1088.     
  1089.         FW_CPoint topLeft(FW_IntToFixed(10), FW_IntToFixed(10));
  1090.         FW_CPoint botRight(FW_IntToFixed(82), FW_IntToFixed(82));
  1091.     
  1092.         theShape->SetShapeGeometry(topLeft, botRight);
  1093.         
  1094.         if (event.HasDataKey(keyAEPropData))
  1095.         {
  1096.             FW_CDesc properties;
  1097.             event.GetDataByDesc(properties, keyAEPropData, typeAERecord);
  1098.             theShape->SetProperties(ev, part, properties);
  1099.         }
  1100.         
  1101.         CDrawShapeCommand* cmd = FW_NEW(CDrawShapeCommand,
  1102.                     (ev, this, FW_MScriptable::GetFrame(ev, part), fDrawSelection, theShape));
  1103.     
  1104.         cmd->Execute(ev);
  1105.     
  1106.         GetDrawContent()->RedrawShape(ev, theShape);
  1107.     
  1108.         FW_CDesc objectSpec;
  1109.     
  1110.         BuildObjectSpecifier(ev, part, FW_MScriptable::GetFrame(ev, part), objectSpec, GetSpecifierForm());
  1111.         reply.PutDataByDesc(objectSpec, keyAEResult);
  1112.     }
  1113.     else
  1114.         FW_MEmbeddingPartScriptable::DoAECreateElement(ev, part, event, reply);
  1115. }
  1116.  
  1117. //----------------------------------------------------------------------------------------
  1118. // CDrawPart::ShapeClassToType
  1119. //----------------------------------------------------------------------------------------
  1120.  
  1121. FW_Boolean CDrawPart::ShapeClassToType(ODDescType shapeClass,
  1122.                                     unsigned short& shapeType) const
  1123. {
  1124.     FW_Boolean result = TRUE;
  1125.     
  1126.     switch(shapeClass)
  1127.     {
  1128.         case cLine:
  1129.             shapeType = kLineShape;
  1130.             break;
  1131.         
  1132.         case cRectangle:
  1133.             shapeType = kRectShape;
  1134.             break;
  1135.         
  1136.         case cOval:
  1137.             shapeType = kOvalShape;
  1138.             break;
  1139.         
  1140.         case cRoundedRectangle:
  1141.             shapeType = kRRectShape;
  1142.             break;
  1143.             
  1144.         case cText:
  1145.             shapeType = kTextShape;
  1146.             break;
  1147.         
  1148.         default:
  1149.             result = FALSE;
  1150.             break;
  1151.     }
  1152.     
  1153.     return result;
  1154. }
  1155.  
  1156.  
  1157. //----------------------------------------------------------------------------------------
  1158. // CDrawPart::EmbeddedFrameRemoved
  1159. //----------------------------------------------------------------------------------------
  1160.  
  1161. void CDrawPart::EmbeddedFrameRemoved(Environment *ev, FW_MProxy* proxy)
  1162. {
  1163.     CProxyShape* proxyShape = (CProxyShape*)proxy;
  1164.     
  1165.     FW_CRect updateBox;
  1166.     proxyShape->GetUpdateBox(updateBox);
  1167.     
  1168.     fMainPresentation->Invalidate(ev, updateBox);
  1169.     
  1170.     // ----- I don't need to call RemoveShapeFromPart because the embedded frames have already been
  1171.     // ----- deleted (ODF calls FW_MProxy::RemoveEmbeddedFrames)
  1172.     fPartContent->RemoveShape(ev, proxyShape);
  1173.     delete proxyShape;
  1174. }
  1175.  
  1176. //----------------------------------------------------------------------------------------
  1177. // CDrawPart::HandleNotification
  1178. //----------------------------------------------------------------------------------------
  1179.  
  1180. void CDrawPart::HandleNotification(Environment* ev, const FW_CNotification& notification)
  1181. {
  1182.     switch (notification.GetMessage()) {
  1183.         case kColorChanged:    
  1184.             {
  1185.                 CColorChangedNotification& colorNotification = (CColorChangedNotification&)notification;
  1186.                 SetColor(ev, colorNotification.GetColor(), colorNotification.GetRenderVerb());
  1187.             }
  1188.             break;
  1189.         case kPatternChanged:    
  1190.             {
  1191.                 CPatternChangedNotification& patternNotification = (CPatternChangedNotification&)notification;
  1192.                 SetPattern(ev, patternNotification.GetPattern(), patternNotification.GetRenderVerb());
  1193.             }
  1194.             break;
  1195.     };
  1196. }
  1197.     
  1198.